<!-- 用到了 -->

<template>
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
  <el-form-item
    prop="questionName"
    label="问题描述"
    :rules="{
      required: true, message: '问题描述不能为空', trigger: 'blur'
    }"
  >
    <el-input v-model="dynamicValidateForm.questionName"></el-input>
  </el-form-item>
  <el-form-item
    prop="type"
    label="问题类型"
  >

<el-select v-model="type" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
  </el-form-item>
   
  <el-form-item
    prop="order"
    label="题号"
     :rules="{
      required: true, message: '题号不能为空', trigger: 'blur'
    }"
  >
    <el-input v-model="dynamicValidateForm.order"></el-input>
  </el-form-item>
  <!-- <el-form-item
    prop="required"
    label="是否必须"
    :rules="{
      required: true, message: '是否必须不能为空', trigger: 'blur'
    }"
  >
    <el-input v-model="dynamicValidateForm.required"></el-input>
  </el-form-item> -->
  <el-form-item label="是否必填">
    <el-select v-model="dynamicValidateForm.required" placeholder="请选择是否必填" >
        <el-option
            v-for="item in StatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
    </el-select>
  </el-form-item>
  <div  v-if="type==1||type==2">
    <el-form-item
    v-for="(domain, index) in dynamicValidateForm.domains"
    :label="'选项名' + (index+1)"
    :key="domain.key"
    :prop="'domains.' + index + '.value'"
    :rules="{
      required: true, message: '选项名不能为空', trigger: 'blur'
    }">
    <el-input v-model="domain.value"></el-input><el-button @click.prevent="removeDomain(domain)">删除该选项</el-button>
  </el-form-item>
  </div>
  
  <el-form-item>
    <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
    <el-button @click="addDomain" v-if="type==1||type==2">新增选项名</el-button>
    <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
     <el-button @click="$router.push('/book')">返回问卷列表</el-button>
  </el-form-item>
</el-form>
</template>
<script>
import axios from 'axios';

  export default {
    data() {
      return {
         StatusList: [
            {
                label: '是',
                value: 1
                },
                {
                label: '否',
                value: 2
            }
          ],
        dynamicValidateForm: {
          domains: [{
            value: ''
          }],
          questionName: '',
          order:'',
          required:1,
          answer:'',
          secondquestionId:''
         
        },
        options: [{
          value: '1',
          label: '单选题'
        }, {
          value: '2',
          label: '多选题'
        }, {
          value: '3',
          label: '简答题'
        }],
        type:'1'
      };
    },
    methods: {
      submitForm(formName) {
         const data={"order":2,"questionName":this.dynamicValidateForm.questionName,"questionnaireId":this.$route.query.id,"type":this.type,"required":2}
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
            var arr=JSON.parse(JSON.stringify(this.dynamicValidateForm.domains))
            var newarr=new Array();
            
           //console.log(arr)
           // console.log(this.dynamicValidateForm.domains.length)
        
           for(var i=0;i<this.dynamicValidateForm.domains.length;i++){
               newarr[i]=this.dynamicValidateForm.domains[i].value
               console.log(this.dynamicValidateForm.domains[i].value)
           }
            const _this=this
             axios({
                  // url:'http://uchiha.vip:5000/question/',
                  url:'/question/',
                  method:"post",
                  data:data,
            }).then(function(resp){
                console.log(resp.data.data)
                const data1=resp.data.data
                  _this.$message({
                  type: "success",
                message: "新增问题成功"
                })
               
            for(var i=0;i<newarr.length;i++){
                    console.log(newarr[i])
                     const optiondata={"optionName":newarr[i],"questionId":data1,"count":0}
                    axios({
                    // url:'http://uchiha.vip:5000/options/',
                    url:'/options/',
                    method:'post',
                    data:optiondata
                }).then(function(resp){
                        console.log(555+resp)
                          _this.$message({
                        type: "success",
                       message: "新增选项成功"
                      })
                })
             }
                
            })  
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      removeDomain(item) {
        var index = this.dynamicValidateForm.domains.indexOf(item)
        if (index !== -1) {
          this.dynamicValidateForm.domains.splice(index, 1)
        }
      },
      addDomain() {
        this.dynamicValidateForm.domains.push({
          value: '',
          key: Date.now()
        });
      },

      // tobook(){
      //   this.$router.push('/book');
      // }

    }
  }
</script>